/**
 * Created by renzhiqiang on 18/7/4.
 */
import * as <%=router%> from '../../services/<%=router%>/index';
import pathToRegexp from 'path-to-regexp';
import {openNotice,adpterFilter,adpterColumns} from '../../public/utils'
export default {
    namespace: '<%=router%>',
    state: {
        list: [], //
        loading:false,
        total: 0,
        filterData:{},
        configPage:{
            buttonInfo:[],
            searchInfo:[]
        },
        pageParam: { //表格所需参数
            pageNo: 1,
            pageSize: 10,
            keyword: '',
            value:'',
            key:''
        }
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload};
        }
    },
    effects: {
        //获取菜单配置参数
        *getConfig({payload}, { call, put, select}) {
            yield put({type: 'save', payload: {...payload}});
            let configPage = yield select(function (state) {return state.<%=router%>.configPage});
            const data= yield call(<%=router%>.getPageConfigureById,payload.MenuId);
            configPage=data.result||{};
            configPage.searchInfo=[];
            for(let i=0;i<configPage.paramFilters.length;i++){
                configPage.searchInfo.push((yield adpterFilter(configPage.paramFilters[i])));
            };
            configPage.tableHeads=adpterColumns(configPage);
            yield put({type: 'save', payload: {configPage:configPage}});
            yield put({type: 'fetch', payload: {}});
        },
        //获取列表
        *fetch({payload}, { call, put, select}) {
            yield put({type: 'save', payload: {...payload}});
            const params = yield select(function (state) {return state.<%=router%>});
            const data= yield call(<%=router%>.fetch,params);
            yield put({
                type: 'save',
                payload: {
                    list: data.result||[],
                    total: parseInt(data.iTotalRecords, 10),
                },
            })
        },
        *del({payload}, { call, put }) {
            let data= yield call(<%=router%>.del,payload.id);
                openNotice('success', '提示', "删除成功");
                yield put({ type: 'reload' });
        },
        *reload(action, { put, select }) {
            yield put({ type: 'fetch', payload: {pageParam: {pageNo: 1, pageSize: 10, keyword: ''}}});
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            //当路由发生变化，会触发这个
            return history.listen(({ pathname, query }) => {
                const match = pathToRegexp('/<%=router%>/:MenuId/:projectId').exec(pathname);
                if (match) { // 初始化参数
                    dispatch({ type: 'getConfig', payload: {MenuId:match[1],configPage:[],pageParam: {pageNo: 1, pageSize: 10,value:'', key:'',keyword:''}}});
                }});
        },
    },
};
